package bj.gui
import javax.swing.event.ChangeEvent
import scala.swing.SimpleGUIApplication
import scala.swing.event
import java.awt.event.ActionListener
import event.ButtonClicked
import scala.swing.Frame
import scala.swing.Label
import scala.swing.Swing
import scala.swing.Button
import scala.swing.FlowPanel
import scala.swing.GridPanel
import scala.swing.Component
import javax.swing.SpinnerNumberModel
import javax.swing.JSpinner
import scala.swing.TextField
import javax.swing.ImageIcon
import javax.swing.event.ChangeListener
import scala.swing.event.ActionEvent

class Login extends Frame {
  javax.swing.UIManager.setLookAndFeel(Constants.LF)
  println("Opened login window")
  title = "Blackjack - Login"
  minimumSize = Constants.LoginSize

  val nameField = new TextField {
    columns = 7
  }

  val moneySpinner = new JSpinner(new SpinnerNumberModel(100.00, 0.00, 5000.00, 20)) with ChangeListener {
	  addChangeListener(this)
	  def stateChanged(e : ChangeEvent) = {
	    val doubleVal = this.getValue().toString().toDouble
	    val intVal = doubleVal.toInt
	    val roundOff = (intVal/20)*20
	    println("round up: " +roundOff.toDouble)
	    removeChangeListener(this) /* Prevent recursive calls to stateChanged */
	    this.setValue(roundOff.toDouble)
	    addChangeListener(this) /* Listen to events again */
	  }
  }
  moneySpinner.setEditor(new JSpinner.NumberEditor(moneySpinner, "0.00"))

  val okButton = new Button { text = "OK" }
  val cancelButton = new Button { text = "Cancel" }

  val welcomePanel = new FlowPanel() {
    contents += new Label { text = "Welcome to Blackjack! Please log in" }
    background = Constants.greenBack
  }
  val namePanel = new FlowPanel() {
    contents += new Label { text = "Player name: " }
    contents += nameField
    background = Constants.greenBack
  }
  val bankPanel = new FlowPanel() {
    contents += new Label { text = "Initial Bankroll: $" }
    contents += Component.wrap(moneySpinner)
    background = Constants.greenBack
  }
  val buttonPanel = new FlowPanel() {
    contents += okButton
    contents += cancelButton
    background = Constants.greenBack
  }
  contents = new FlowPanel() {
    contents += new Label {
      icon = new ImageIcon("images/BJLogo2.gif")
    }
    contents += new GridPanel(4, 1) {
      contents += welcomePanel
      contents += namePanel
      contents += bankPanel
      contents += buttonPanel
    }
    border = Swing.EmptyBorder(20)
    background = Constants.greenBack
  }
  listenTo(okButton)
  listenTo(cancelButton)
  reactions += {
    case ButtonClicked(`okButton`) =>
      println("Clicked ok button")
      if (nameField.text == "")
        new BjInfoBox(new Label("Please enter your name"))
      else {
      val addS = moneySpinner.getValue().toString()
      val d : Double = addS.toDouble      // Invoke method logIn in Lobby to send a message to the House actor to create a new HumanPlayer using information from this GUI
      Lobby.top.logIn(nameField.text,d)
      this.close()
      }
    case ButtonClicked(`cancelButton`) =>
      this.close()
  }
  visible = true
}
